package thread.thread3;

import java.util.Random;

public class MoneyTaker extends Thread {
	private Account ac;

	public MoneyTaker(String name, Account ac) {
		super(name);
		this.ac = ac;
	}

	@Override
	public void run() {
		while (true) {
			int m = new Random().nextInt(400);
			System.out.println(getName() + "准备取钱" + m + "元");
			synchronized (ac) {
				if (ac.getBalance() >= m) {
					try {
						sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					ac.takeMoney(m);
				} else {
					System.out.println(getName() + "取钱" + m + "失败");
				}
			}
			System.out.println("当前账户余额为：" + ac.getBalance() + "元");
			if (ac.getBalance() <= 0) {
				break;
			}
		}
	}
}
